﻿using Ext.Net;
using NOAH.CMS.Repository;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class admin_Modules_NewsEvent_Subject_SortCategory : System.Web.UI.Page
{
    protected void LoadOrderForm()
    {
        using (SubjectRepository repository = new SubjectRepository())
        {
            string cates = "";
            List<Subject> allCate = (from p in repository.GetAll()
                                     orderby p.Order
                                     select p).ToList<Subject>();
            foreach (Subject cate in allCate)
            {
                cates = cates + string.Format("<li id='{0}'>{1}</li>", cate.ID.ToString(), cate.Name);
            }
            string output = " <div class='demo'>\r\n                            <ul id='sortable'>\r\n                             {0}\r\n                            </ul>\r\n                        </div>";
            string script = " $(function () {\r\n                                $('#sortable').sortable();\r\n                                $('#sortable').disableSelection();\r\n                            });";
            this.lblCategoryOrder.Text = string.Format(output, cates);
            this.SM.RegisterClientScriptBlock("load", script);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ExtNet.IsAjaxRequest)
        {
            this.LoadOrderForm();
        }
    }

    public void UpdateNewsCategoryOrder(object sender, DirectEventArgs e)
    {
        SubjectRepository repository = new SubjectRepository();
        try
        {
            string order = e.ExtraParams["order"];
            List<Subject> allCate = repository.GetAll();
            if (allCate.Count == 0)
            {
                Util.ShowMessage2("Kh\x00f4ng c\x00f3 chuy\x00ean mục n\x00e0o đế sắp xếp!");
            }
            else
            {
                short k = 0;
                foreach (string id in order.Split(new char[] { ',' }))
                {
                    allCate.Find(p => p.ID.ToString() == id).Order = new short?(k);
                    k = (short)(k + 1);
                }
                repository.SubmitChanges();
                BasePage.RegisterCallBackFromChild(this.SM, string.Empty);
                BasePage.RegisterCloseWindow(this.SM);
            }
        }
        catch
        {
            Util.ShowMessage2(Constants.UPDATE_FAILURE);
        }
        finally
        {
            if (repository != null)
            {
                repository.Dispose();
            }
        }
    }
}